<!--
    EAC-CPF to XTM2.0 converter Version 0.1 - 20090913
    
    @author Salvatore Vassallo <vassallosalvatore at gmail.com>
    
   
    Copyright (c) 2009, Salvatore Vassallo
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
    * Neither the name of the Space Applications Services nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.
    
    THIS SOFTWARE IS PROVIDED BY SPACE APPLICATIONS SERVICES ``AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL SPACE APPLICATIONS SERVICES BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
    
    Changelog
    0.1 - First attempt (Isaar identity area, description area, control Area) 
    
    TODO
    html in resourceData?
    outline
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xtm="http://www.topicmaps.org/xtm/" xmlns:eac="http://eac.staatsbibliothek-berlin.de"
    exclude-result-prefixes="xs eac xsl xlink" version="2.0">

    <xsl:output method="xml" indent="yes" encoding="UTF-8"
        cdata-section-elements="xtm:value xtm:resourceData"/>

    <xsl:template match="/">
        <xtm:topicMap version="2.0" reifier="#eac-cpf-import">
            <xtm:topic id="eac-cpf-import">
                <xtm:instanceOf>
                    <xtm:topicRef href="#system001"/>
                </xtm:instanceOf>
                <xtm:name>
                    <xtm:value>Export of EAC-CPF</xtm:value>
                </xtm:name>
                <xtm:occurrence>
                    <xtm:type>
                        <xtm:topicRef href="#system007"/>
                    </xtm:type>
                    <xtm:resourceData>Conversion of eac-cpf record(s) using
                        eac2tm</xtm:resourceData>
                </xtm:occurrence>
                <xtm:occurrence>
                    <xtm:type>
                        <xtm:topicRef href="#system009"/>
                    </xtm:type>
                    <xtm:resourceRef href="http://code.google.com/p/tm4bad/"/>
                </xtm:occurrence>
            </xtm:topic>

            <xsl:apply-templates select="//eac:eac-cpf"/>

            <!-- create topic for rules, try deduplication based on abbreviation -->
            <xsl:for-each-group select="//eac:conventionDeclaration[eac:citation/text()]" group-by="eac:citation">
                <xsl:call-template name="rule"/>
            </xsl:for-each-group>

            <!-- create topic for language, try deduplication based  -->
            <xsl:for-each-group select="//eac:language" group-by="@languageCode">
                <xsl:call-template name="language"/>
            </xsl:for-each-group>

            <!-- create topic for script, try deduplication based  -->
            <xsl:for-each-group select="//eac:script" group-by="@scriptCode">
                <xsl:call-template name="script"/>
            </xsl:for-each-group>

            <!-- create one topic - date for every date, fromDate and toDate element, try deduplication -->
            <!-- At the moment I think that is better check all elements... the risk of collusion is too high -->
            <xsl:for-each-group
                select="//eac:date[not(ancestor::eac:chronItem)]|//eac:fromDate[not(ancestor::eac:chronItem)]|//eac:toDate[not(ancestor::eac:chronItem)]"
                group-by="replace(concat('s',@standardDate,'nb',@notBefore,'na',@notAfter,'-',.),'[^-_.:A-Za-z0-9]','')">
                <xsl:call-template name="date">
                    <xsl:with-param name="dataId" select="current-grouping-key()"/>
                </xsl:call-template>
            </xsl:for-each-group>

            <!-- Create period topic for every dataRange, try deduplication -->
            <xsl:for-each-group select="//eac:dateRange[not(ancestor::eac:chronItem)]"
                group-by="replace(concat('period-s',eac:fromDate/@standardDate,'nb',eac:fromDate/@notBefore,'na',eac:fromDate/@notAfter,'-','s',eac:toDate/@standardDate,'nb',eac:toDate/@notBefore,'na',eac:toDate/@notAfter,'-',eac:toDate),'[^-_.:A-Za-z0-9]','')">
                <xsl:call-template name="period">
                    <xsl:with-param name="periodId" select="current-grouping-key()"/>
                </xsl:call-template>
            </xsl:for-each-group>

            <!-- create one topic - place for every place element, try deduplication -->
            <xsl:for-each-group select="//eac:place" group-by=".">
                <xsl:call-template name="place"/>
            </xsl:for-each-group>

            <!-- create one topic - legal status for every //legalstatus/term element, try deduplication -->
            <xsl:for-each-group select="//eac:legalStatus/eac:term " group-by=".">
                <xsl:call-template name="termTopic">
                    <xsl:with-param name="termId"
                        select="concat('legal-',replace(.,'[^-_.:A-Za-z0-9]',''))"/>
                    <xsl:with-param name="type" select="'eac-cpf015'"/>
                </xsl:call-template>
            </xsl:for-each-group>

            <!-- function -->
            <xsl:for-each-group select="//eac:function/eac:term " group-by=".">
                <xsl:call-template name="termTopic">
                    <xsl:with-param name="termId"
                        select="concat('function-',replace(.,'[^-_.:A-Za-z0-9]',''))"/>
                    <xsl:with-param name="type" select="'eac-cpf019'"/>
                </xsl:call-template>
            </xsl:for-each-group>

            <!-- occupation -->
            <xsl:for-each-group select="//eac:occupation/eac:term " group-by=".">
                <xsl:call-template name="termTopic">
                    <xsl:with-param name="termId"
                        select="concat('occupation-',replace(.,'[^-_.:A-Za-z0-9]',''))"/>
                    <xsl:with-param name="type" select="'eac-cpf020'"/>
                </xsl:call-template>
            </xsl:for-each-group>

            <!-- activity -->
            <xsl:for-each-group select="//eac:activity/eac:term " group-by=".">
                <xsl:call-template name="termTopic">
                    <xsl:with-param name="termId"
                        select="concat('activity-',replace(.,'[^-_.:A-Za-z0-9]',''))"/>
                    <xsl:with-param name="type" select="'eac-cpf021'"/>
                </xsl:call-template>
            </xsl:for-each-group>

            <!-- mandate -->
            <xsl:for-each-group select="//eac:mandate/eac:term" group-by=".">
                <xsl:call-template name="termTopic">
                    <xsl:with-param name="termId"
                        select="concat('mandate-',replace(.,'[^-_.:A-Za-z0-9]',''))"/>
                    <xsl:with-param name="type" select="'eac-cpf023'"/>
                </xsl:call-template>
            </xsl:for-each-group>

            <xsl:call-template name="eac-construct"/>
            <xsl:call-template name="system-construct"/>

        </xtm:topicMap>
    </xsl:template>

    <xsl:template match="eac:eac-cpf">
        <!-- TODO multipleIdentities -->
        <xsl:apply-templates select="eac:cpfDescription"/>
    </xsl:template>

    <!-- dates -->
    <xsl:template name="date">
        <xsl:param name="dataId"/>
        <xtm:topic id="date-{$dataId}">
            <xsl:if test="@standardDate">
                <xtm:subjectIdentifier href="http://psi.semagia.com/iso8601/{@standardDate}"/>
            </xsl:if>
            <xtm:instanceOf>
                <xtm:topicRef href="#system010"/>
            </xtm:instanceOf>
            <xsl:apply-templates select="@standardDate"/>
            <xsl:apply-templates select="." mode="date"/>
            <xsl:if test="@notBefore|@notAfter">
                <xtm:name>
                    <xtm:value>
                        <xsl:apply-templates select="@notBefore"/>
                        <xsl:apply-templates select="@notAfter"/>
                    </xtm:value>
                </xtm:name>
            </xsl:if>
        </xtm:topic>
    </xsl:template>

    <!-- periods -->
    <!-- todo: clean up code: too many if -->
    <xsl:template name="period">
        <xsl:param name="periodId"/>
        <xtm:topic id="{$periodId}">
            <xtm:instanceOf>
                <xtm:topicRef href="#system012"/>
            </xtm:instanceOf>
            <xsl:if test=".//@standardDate">
                <xtm:name>
                    <xtm:scope>
                        <xtm:topicRef href="#system011"/>
                    </xtm:scope>
                    <xtm:value>
                        <xsl:value-of
                            select="concat(eac:fromDate/@standardDate,' - ',eac:toDate/@standardDate)"
                        />
                    </xtm:value>
                </xtm:name>
            </xsl:if>
            <xsl:if test="eac:toDate/text()|eac:fromDate/text()">
                <xtm:name>
                    <xtm:value>
                        <xsl:value-of select="concat(eac:fromDate,' - ',eac:toDate)"/>
                    </xtm:value>
                </xtm:name>
            </xsl:if>
            <xsl:if test=".//@notBefor|.//@notAfter">
                <xtm:name>
                    <xtm:value>
                        <xsl:apply-templates select="eac:fromDate/@notBefore"/>
                        <xsl:apply-templates select="eac:fromDate/@notAfter"/>
                        <xsl:if test="eac:toDate/@notAfter|eac:toDate/@notAfter">
                            <xsl:text> - </xsl:text>
                            <xsl:apply-templates select="eac:toDate/@notBefore"/>
                            <xsl:apply-templates select="eac:toDate/@notAfter"/>
                        </xsl:if>
                    </xtm:value>
                </xtm:name>
            </xsl:if>
        </xtm:topic>
        <xsl:apply-templates mode="period">
            <xsl:with-param name="periodId" select="$periodId"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="@standardDate">
        <xtm:name>
            <xtm:scope>
                <xtm:topicRef href="#system011"/>
            </xtm:scope>
            <xtm:value>
                <xsl:value-of select="."/>
            </xtm:value>
        </xtm:name>
    </xsl:template>
    <xsl:template match="@notAfter">
        <xsl:value-of select="concat('before ',.,' ')"/>
    </xsl:template>
    <xsl:template match="@notBefore">
        <xsl:value-of select="concat('after ' ,.,' ')"/>
    </xsl:template>
    <xsl:template match="eac:date|eac:toDate|eac:fromDate" mode="date">
        <xsl:if test="text()">
            <xtm:name>
                <xtm:value>
                    <xsl:value-of select="."/>
                </xtm:value>
            </xtm:name>
        </xsl:if>
    </xsl:template>
    <xsl:template match="eac:fromDate" mode="period">
        <xsl:param name="periodId"/>
        <xtm:association>
            <xtm:type>
                <xtm:topicRef href="#system013"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system010"/>
                </xtm:type>
                <xtm:topicRef
                    href="#{replace(concat('date-s',@standardDate,'nb',@notBefore,'na',@notAfter,'-',.),'[^-_.:A-Za-z0-9]','')}"
                />
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system012"/>
                </xtm:type>
                <xtm:topicRef href="#{$periodId}"/>
            </xtm:role>
        </xtm:association>
    </xsl:template>
    <xsl:template match="eac:toDate" mode="period">
        <xsl:param name="periodId"/>
        <xtm:association>
            <xtm:type>
                <xtm:topicRef href="#system014"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system010"/>
                </xtm:type>
                <xtm:topicRef
                    href="#{replace(concat('date-s',@standardDate,'nb',@notBefore,'na',@notAfter,'-',.),'[^-_.:A-Za-z0-9]','')}"
                />
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system012"/>
                </xtm:type>
                <xtm:topicRef href="#{$periodId}"/>
            </xtm:role>
        </xtm:association>
    </xsl:template>

    <!-- Isaar 5.1 5.2 5.4  -->
    <xsl:template match="eac:cpfDescription">
        <xsl:variable name="recordId"
            select="../eac:control/eac:recordId|../../eac:control/eac:recordId"/>
        <xsl:variable name="identityId" select="concat('identity-',position(),'-',$recordId)"/>
        <xtm:topic id="{$identityId}">
            <!-- isaar 5.1 -->
            <xsl:apply-templates select="eac:identity"/>

            <!-- isaar 5.2.2 -->
            <xsl:apply-templates select="eac:description/eac:biogHist"/>

            <!-- isaar 5.2.7 -->
            <xsl:apply-templates select="eac:description/eac:structureOrGenealogy"/>

            <!-- isaar 5.2.8 -->
            <xsl:apply-templates select="eac:description/eac:generalContext"/>

            <!-- isaar 5.4.1 -->
            <xtm:occurrence reifier="#control-{$recordId}">
                <xtm:type>
                    <xtm:topicRef href="#eac-cpf001"/>
                </xtm:type>
                <xtm:resourceData datatype="http://www.w3.org/2001/XMLSchema#string">
                    <xsl:value-of select="$recordId"/>
                </xtm:resourceData>
            </xtm:occurrence>
        </xtm:topic>

        <!-- isaar 5.2.1 -->
        <xsl:apply-templates select="eac:description/eac:existDates">
            <xsl:with-param name="identityId" select="$identityId"/>
        </xsl:apply-templates>

        <!-- isaar 5.2.3 -->
        <xsl:apply-templates select=".//eac:placeDate">
            <xsl:with-param name="identityId" select="$identityId"/>
        </xsl:apply-templates>

        <!-- isaar 5.2.4 5.2.5 5.2.6-->
        <xsl:apply-templates select=".//eac:legalStatus">
            <xsl:with-param name="identityId" select="$identityId"/>
        </xsl:apply-templates>
        <xsl:apply-templates select=".//eac:function">
            <xsl:with-param name="identityId" select="$identityId"/>
        </xsl:apply-templates>
        <xsl:apply-templates select=".//eac:occupation">
            <xsl:with-param name="identityId" select="$identityId"/>
        </xsl:apply-templates>
        <xsl:apply-templates select=".//eac:activity">
            <xsl:with-param name="identityId" select="$identityId"/>
        </xsl:apply-templates>
        <xsl:apply-templates select=".//eac:mandate">
            <xsl:with-param name="identityId" select="$identityId"/>
        </xsl:apply-templates>

        <!-- isaar 5.4 -->
        <xsl:apply-templates select="../eac:control|../../eac:control"/>

    </xsl:template>

    <!-- isaar 5.1 -->
    <xsl:template match="eac:identity">
        <!-- topic type -->
        <xtm:instanceOf>
            <xtm:topicRef href="#eac-cpf-{eac:entityType}"/>
        </xtm:instanceOf>
        <!-- Name -->
        <xsl:apply-templates select=".//eac:nameEntry"/>
        <!-- todo find a better solution for parallel name -->
    </xsl:template>
    <xsl:template match="eac:nameEntry">
        <xsl:variable name="aARule" select="@authorizedByRules"/>
        <xsl:variable name="aRule" select="@rules"/>
        <xtm:name>
            <xsl:if
                test="useDates//eac:date or useDates//eac:dateRange or //eac:abbreviation=$aARule or //eac:abbreviation=$aRule">
                <xtm:scope>
                    <xsl:for-each select=".//eac:date">
                        <xtm:topicRef
                            href="#{replace(concat('date-s',@standardDate,'nb',@notBefore,'na',@notAfter,'-',.),'[^-_.:A-Za-z0-9]','')}"
                        />
                    </xsl:for-each>
                    <xsl:for-each select=".//eac:dateRange">
                        <xtm:topicRef
                            href="#{replace(concat('period-s',eac:fromDate/@standardDate,'nb',eac:fromDate/@notBefore,'na',eac:fromDate/@notAfter,'-','s',eac:toDate/@standardDate,'nb',eac:toDate/@notBefore,'na',eac:toDate/@notAfter,'-',eac:toDate),'[^-_.:A-Za-z0-9]','')}"
                        />
                    </xsl:for-each>
                    <xsl:if test="//eac:abbreviation=$aARule">
                        <xtm:topicRef
                            href="#{concat('rule-',replace(ancestor::eac:eac-cpf/eac:control/eac:conventionDeclaration/eac:citation[../eac:abbreviation=$aARule],'[^-_.:A-Za-z0-9]',''))}"
                        />
                    </xsl:if>
                    <xsl:if test="//eac:abbreviation=$aRule and $aARule!=$aRule">
                        <xtm:topicRef
                            href="#{concat('rule-',replace(ancestor::eac:eac-cpf/eac:conventionDeclaration/eac:citation[../eac:abbreviation=$aRule],'[^-_.:A-Za-z0-9]',''))}"
                        />
                    </xsl:if>
                </xtm:scope>
            </xsl:if>
            <xtm:value>
                <xsl:apply-templates select="eac:part"/>
            </xtm:value>
        </xtm:name>
    </xsl:template>
    <xsl:template match="eac:part">
        <xsl:value-of select="concat(.,' ')"/>
    </xsl:template>

    <!-- isaar 5.2 -->
    <!-- isaar 5.2.1 -->
    <xsl:template match="eac:existDates//eac:date">
        <xsl:param name="identityId"/>
        <xsl:variable name="assoId" select="concat('asso-date',position())"/>
        <xsl:element name="xtm:association">
            <xsl:if test="eac:descriptiveNote">
                <xsl:attribute name="reifier">#<xsl:value-of select="$assoId"/></xsl:attribute>
            </xsl:if>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf017"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#eac-cpf-agent"/>
                </xtm:type>
                <xtm:topicRef href="#{$identityId}"/>
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system010"/>
                </xtm:type>
                <xtm:topicRef
                    href="#{replace(concat('date-s',@standardDate,'nb',@notBefore,'na',@notAfter,'-',.),'[^-_.:A-Za-z0-9]','')}"
                />
            </xtm:role>
        </xsl:element>
        <xsl:if test="eac:descriptiveNote or eac:citation">
            <xsl:call-template name="reifier-asso">
                <xsl:with-param name="assoId" select="$assoId"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <xsl:template match="eac:existDates//eac:dateRange">
        <xsl:param name="identityId"/>
        <xsl:variable name="assoId" select="concat('asso-period',position())"/>
        <xsl:element name="xtm:association">
            <xsl:if test="eac:descriptiveNote">
                <xsl:attribute name="reifier">#<xsl:value-of select="$assoId"/></xsl:attribute>
            </xsl:if>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf017"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#eac-cpf-agent"/>
                </xtm:type>
                <xtm:topicRef href="#{$identityId}"/>
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system012"/>
                </xtm:type>
                <xtm:topicRef
                    href="#{replace(concat('period-s',eac:fromDate/@standardDate,'nb',eac:fromDate/@notBefore,'na',eac:fromDate/@notAfter,'-','s',eac:toDate/@standardDate,'nb',eac:toDate/@notBefore,'na',eac:toDate/@notAfter,'-',eac:toDate),'[^-_.:A-Za-z0-9]','')}"
                />
            </xtm:role>
        </xsl:element>
        <xsl:if test="eac:descriptiveNote or eac:citation">
            <xsl:call-template name="reifier-asso">
                <xsl:with-param name="assoId" select="$assoId"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <!-- isaar 5.2.2 -->
    <xsl:template match="eac:biogHist">
        <xtm:occurrence>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf008"/>
            </xtm:type>
            <xtm:resourceData>
                <xsl:apply-templates/>
            </xtm:resourceData>
        </xtm:occurrence>
    </xsl:template>

    <!-- isaar 5.2.3 -->
    <xsl:template match="eac:placeDate">
        <!-- only association, topics are created by template "place" -->
        <xsl:param name="identityId"/>
        <xsl:variable name="assoId" select="concat('asso-place',position())"/>
        <xsl:element name="xtm:association">
            <xsl:if test="eac:descriptiveNote|eac:citation">
                <xsl:attribute name="reifier">#<xsl:value-of select="$assoId"/></xsl:attribute>
            </xsl:if>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf014"/>
            </xtm:type>
            <xsl:if test=".//eac:date or .//eac:dataRange">
                <xtm:scope>
                    <xsl:for-each select=".//eac:date">
                        <xtm:topicRef
                            href="#{replace(concat('date-s',@standardDate,'nb',@notBefore,'na',@notAfter,'-',.),'[^-_.:A-Za-z0-9]','')}"
                        />
                    </xsl:for-each>
                    <xsl:for-each select=".//eac:dateRange">
                        <xtm:topicRef
                            href="#{replace(concat('period-s',eac:fromDate/@standardDate,'nb',eac:fromDate/@notBefore,'na',eac:fromDate/@notAfter,'-','s',eac:toDate/@standardDate,'nb',eac:toDate/@notBefore,'na',eac:toDate/@notAfter,'-',eac:toDate),'[^-_.:A-Za-z0-9]','')}"
                        />
                    </xsl:for-each>
                </xtm:scope>
            </xsl:if>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#eac-cpf-agent"/>
                </xtm:type>
                <xtm:topicRef href="#{$identityId}"/>
            </xtm:role>
            <xsl:for-each select="eac:place">
                <xtm:role>
                    <xtm:type>
                        <xtm:topicRef href="#eac-cpf010"/>
                    </xtm:type>
                    <xtm:topicRef href="#place-{replace(.,'[^-_.:A-Za-z0-9]','')}"/>
                </xtm:role>
            </xsl:for-each>
        </xsl:element>
        <xsl:if test="eac:descriptiveNote or eac:citation">
            <xsl:call-template name="reifier-asso">
                <xsl:with-param name="assoId" select="$assoId"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <!-- places -->
    <xsl:template name="place">
        <!-- todo find a better solution for palce without text... -->
        <xsl:if test="text()">
            <xtm:topic id="place-{replace(.,'[^-_.:A-Za-z0-9]','')}">
                <xtm:instanceOf>
                    <xtm:topicRef href="#eac-cpf010"/>
                </xtm:instanceOf>
                <xtm:name>
                    <xtm:value>
                        <xsl:value-of select="."/>
                    </xtm:value>
                </xtm:name>
                <xsl:apply-templates select="@altitude"/>
                <xsl:apply-templates select="@latitude"/>
                <xsl:apply-templates select="@longitude"/>
            </xtm:topic>
        </xsl:if>
    </xsl:template>
    <xsl:template match="@altitude">
        <xtm:occurrence>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf011"/>
            </xtm:type>
            <xtm:resourceData>
                <xsl:value-of select="."/>
            </xtm:resourceData>
        </xtm:occurrence>
    </xsl:template>
    <xsl:template match="@latitude">
        <xtm:occurrence>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf012"/>
            </xtm:type>
            <xtm:resourceData>
                <xsl:value-of select="."/>
            </xtm:resourceData>
        </xtm:occurrence>
    </xsl:template>
    <xsl:template match="@longitude">
        <xtm:occurrence>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf013"/>
            </xtm:type>
            <xtm:resourceData>
                <xsl:value-of select="."/>
            </xtm:resourceData>
        </xtm:occurrence>
    </xsl:template>

    <!-- isaar 5.2.4 -->
    <xsl:template match="eac:legalStatus">
        <xsl:param name="identityId"/>
        <xsl:variable name="assoId" select="concat('legal-asso-',position())"/>

        <xsl:variable name="legalId">
            <xsl:choose>
                <xsl:when test="eac:term">
                    <xsl:value-of select="concat('legal-',replace(eac:term,'[^-_.:A-Za-z0-9]',''))"
                    />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat('legal-',position(),'-',$identityId)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:if test="not(eac:term)">
            <xtm:topic id="{$legalId}">
                <xtm:instanceOf>
                    <xtm:topicRef href="#eac-cpf015"/>
                </xtm:instanceOf>
                <xtm:name>
                    <xtm:value>
                        <xsl:value-of
                            select="concat('Legal status ',position(),' associated to ',$identityId,' record')"
                        />
                    </xtm:value>
                </xtm:name>
                <xsl:apply-templates select="eac:descriptiveNote" mode="occurrence"/>
                <xsl:apply-templates select="eac:citation" mode="occurrence"/>
            </xtm:topic>
        </xsl:if>

        <xsl:call-template name="description-asso">
            <xsl:with-param name="identityId" select="$identityId"/>
            <xsl:with-param name="assoId" select="$assoId"/>
            <xsl:with-param name="termId" select="$legalId"/>
            <xsl:with-param name="assoType" select="'eac-cpf016'"/>
            <xsl:with-param name="type" select="'eac-cpf015'"/>
        </xsl:call-template>
    </xsl:template>

    <!-- isaar 5.2.5 -->
    <xsl:template match="eac:function">
        <xsl:param name="identityId"/>
        <xsl:variable name="assoId" select="concat('function-asso-',position())"/>

        <xsl:variable name="functionId">
            <xsl:choose>
                <xsl:when test="eac:term">
                    <xsl:value-of
                        select="concat('function-',replace(eac:term,'[^-_.:A-Za-z0-9]',''))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat('function-',position(),'-',$identityId)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:if test="not(eac:term)">
            <xtm:topic id="{$functionId}">
                <xtm:instanceOf>
                    <xtm:topicRef href="#eac-cpf019"/>
                </xtm:instanceOf>
                <xtm:name>
                    <xtm:value>
                        <xsl:value-of
                            select="concat('Function ',position(),' associated to ',$identityId,' record')"
                        />
                    </xtm:value>
                </xtm:name>
                <xsl:apply-templates select="eac:descriptiveNote" mode="occurrence"/>
                <xsl:apply-templates select="eac:citation" mode="occurrence"/>
            </xtm:topic>
        </xsl:if>

        <xsl:call-template name="description-asso">
            <xsl:with-param name="identityId" select="$identityId"/>
            <xsl:with-param name="assoId" select="$assoId"/>
            <xsl:with-param name="termId" select="$functionId"/>
            <xsl:with-param name="assoType" select="'eac-cpf018'"/>
            <xsl:with-param name="type" select="'eac-cpf019'"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="eac:occupation">
        <xsl:param name="identityId"/>
        <xsl:variable name="assoId" select="concat('occupation-asso-',position())"/>

        <xsl:variable name="occupationId">
            <xsl:choose>
                <xsl:when test="eac:term">
                    <xsl:value-of
                        select="concat('occupation-',replace(eac:term,'[^-_.:A-Za-z0-9]',''))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat('occupation-',position(),'-',$identityId)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:if test="not(eac:term)">
            <xtm:topic id="{$occupationId}">
                <xtm:instanceOf>
                    <xtm:topicRef href="#eac-cpf020"/>
                </xtm:instanceOf>
                <xtm:name>
                    <xtm:value>
                        <xsl:value-of
                            select="concat('Occupation ',position(),' associated to ',$identityId,' record')"
                        />
                    </xtm:value>
                </xtm:name>
                <xsl:apply-templates select="eac:descriptiveNote" mode="occurrence"/>
                <xsl:apply-templates select="eac:citation" mode="occurrence"/>
            </xtm:topic>
        </xsl:if>

        <xsl:call-template name="description-asso">
            <xsl:with-param name="identityId" select="$identityId"/>
            <xsl:with-param name="assoId" select="$assoId"/>
            <xsl:with-param name="termId" select="$occupationId"/>
            <xsl:with-param name="assoType" select="'eac-cpf018'"/>
            <xsl:with-param name="type" select="'eac-cpf020'"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="eac:activity">
        <xsl:param name="identityId"/>
        <xsl:variable name="assoId" select="concat('activity-asso-',position())"/>
        <xsl:variable name="activityId">
            <xsl:choose>
                <xsl:when test="eac:term">
                    <xsl:value-of
                        select="concat('activity-',replace(eac:term,'[^-_.:A-Za-z0-9]',''))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat('activity-',position(),'-',$identityId)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:if test="not(eac:term)">
            <xtm:topic id="{$activityId}">
                <xtm:instanceOf>
                    <xtm:topicRef href="#eac-cpf021"/>
                </xtm:instanceOf>
                <xtm:name>
                    <xtm:value>
                        <xsl:value-of
                            select="concat('Activity ',position(),' associated to ',$identityId,' record')"
                        />
                    </xtm:value>
                </xtm:name>
                <xsl:apply-templates select="eac:descriptiveNote" mode="occurrence"/>
                <xsl:apply-templates select="eac:citation" mode="occurrence"/>
            </xtm:topic>
        </xsl:if>

        <xsl:call-template name="description-asso">
            <xsl:with-param name="identityId" select="$identityId"/>
            <xsl:with-param name="assoId" select="$assoId"/>
            <xsl:with-param name="termId" select="$activityId"/>
            <xsl:with-param name="assoType" select="'eac-cpf018'"/>
            <xsl:with-param name="type" select="'eac-cpf021'"/>
        </xsl:call-template>
    </xsl:template>

    <!-- isaar 5.2.6 -->
    <xsl:template match="eac:mandate">
        <xsl:param name="identityId"/>
        <xsl:variable name="assoId" select="concat('mandate-asso-',position())"/>
        <xsl:variable name="mandateId">
            <xsl:choose>
                <xsl:when test="eac:term">
                    <xsl:value-of
                        select="concat('mandate-',replace(eac:term,'[^-_.:A-Za-z0-9]',''))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat('mandate-',position(),'-',$identityId)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:if test="not(eac:term)">
            <xtm:topic id="{$mandateId}">
                <xtm:instanceOf>
                    <xtm:topicRef href="#eac-cpf023"/>
                </xtm:instanceOf>
                <xtm:name>
                    <xtm:value>
                        <xsl:value-of
                            select="concat('Mandate ',position(),' associated to ',$identityId,' record')"
                        />
                    </xtm:value>
                </xtm:name>
                <xsl:apply-templates select="eac:descriptiveNote" mode="occurrence"/>
                <xsl:apply-templates select="eac:citation" mode="occurrence"/>
            </xtm:topic>
        </xsl:if>

        <xsl:call-template name="description-asso">
            <xsl:with-param name="identityId" select="$identityId"/>
            <xsl:with-param name="assoId" select="$assoId"/>
            <xsl:with-param name="termId" select="$mandateId"/>
            <xsl:with-param name="assoType" select="'eac-cpf022'"/>
            <xsl:with-param name="type" select="'eac-cpf023'"/>
        </xsl:call-template>
    </xsl:template>
    <!-- isaar 5.2.7 -->
    <xsl:template match="eac:structureOrGenealogy">
        <xtm:occurrence>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf024"/>
            </xtm:type>
            <xtm:resourceData>
                <xsl:apply-templates/>
            </xtm:resourceData>
        </xtm:occurrence>
    </xsl:template>

    <!-- isaar 5.2.8 -->
    <xsl:template match="eac:generalContext">
        <xtm:occurrence>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf009"/>
            </xtm:type>
            <xtm:resourceData>
                <xsl:apply-templates/>
            </xtm:resourceData>
        </xtm:occurrence>
    </xsl:template>

    <!-- general for all 5.2-->
    <xsl:template name="termTopic">
        <xsl:param name="termId"/>
        <xsl:param name="type"/>
        <xtm:topic id="{$termId}">
            <xtm:instanceOf>
                <xtm:topicRef href="#{$type}"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>
                    <xsl:value-of select="."/>
                </xtm:value>
            </xtm:name>
            <xsl:apply-templates select="@xlink:href"/>
        </xtm:topic>
    </xsl:template>

    <xsl:template name="description-asso">
        <xsl:param name="assoId"/>
        <xsl:param name="assoType"/>
        <xsl:param name="type"/>
        <xsl:param name="identityId"/>
        <xsl:param name="termId"/>
        <xsl:element name="xtm:association">
            <xsl:if test="(eac:descriptiveNote|eac:citation) and eac:term">
                <xsl:attribute name="reifier">#<xsl:value-of select="$assoId"/></xsl:attribute>
            </xsl:if>
            <xtm:type>
                <xtm:topicRef href="#{$assoType}"/>
            </xtm:type>
            <xsl:if test="eac:place or .//eac:date or .//eac:dataRange">
                <xtm:scope>
                    <xsl:for-each select="eac:place">
                        <xtm:topicRef href="#place-{replace(.,'[^-_.:A-Za-z0-9]','')}"/>
                    </xsl:for-each>
                    <xsl:for-each select=".//eac:date">
                        <xtm:topicRef
                            href="#{replace(concat('date-s',@standardDate,'nb',@notBefore,'na',@notAfter,'-',.),'[^-_.:A-Za-z0-9]','')}"
                        />
                    </xsl:for-each>
                    <xsl:for-each select=".//eac:dateRange">
                        <xtm:topicRef
                            href="#{replace(concat('period-s',eac:fromDate/@standardDate,'nb',eac:fromDate/@notBefore,'na',eac:fromDate/@notAfter,'-','s',eac:toDate/@standardDate,'nb',eac:toDate/@notBefore,'na',eac:toDate/@notAfter,'-',eac:toDate),'[^-_.:A-Za-z0-9]','')}"
                        />
                    </xsl:for-each>
                </xtm:scope>
            </xsl:if>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#eac-cpf-agent"/>
                </xtm:type>
                <xtm:topicRef href="#{$identityId}"/>
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#{$type}"/>
                </xtm:type>
                <xtm:topicRef href="#{$termId}"/>
            </xtm:role>
        </xsl:element>
        <xsl:if test="eac:descriptiveNote or eac:citation">
            <xsl:call-template name="reifier-asso">
                <xsl:with-param name="assoId" select="$assoId"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="reifier-asso">
        <xsl:param name="assoId"/>
        <xtm:topic id="{$assoId}">
            <xtm:name>
                <xtm:value>Details on Association with association <xsl:value-of select="$assoId"
                    /></xtm:value>
            </xtm:name>
            <xsl:apply-templates select="eac:descriptiveNote" mode="occurrence"/>
            <xsl:apply-templates select="eac:citation" mode="occurrence"/>
        </xtm:topic>
    </xsl:template>
    <xsl:template match="eac:descriptiveNote" mode="occurrence">
        <xtm:occurrence>
            <xtm:type>
                <xtm:topicRef href="#system007"/>
            </xtm:type>
            <xtm:resourceData>
                <xsl:apply-templates/>
            </xtm:resourceData>
        </xtm:occurrence>
    </xsl:template>
    <xsl:template match="eac:citation" mode="occurrence">
        <xtm:occurrence>
            <xtm:type>
                <xtm:topicRef href="#system008"/>
            </xtm:type>
            <xtm:resourceData>
                <xsl:apply-templates/>
            </xtm:resourceData>
        </xtm:occurrence>
    </xsl:template>

    <!-- isaar 5.4 -->
    <xsl:template match="eac:control">
        <xsl:variable name="recordId" select="eac:recordId"/>
        <xtm:topic id="control-{$recordId}">
            <xtm:name>
                <xtm:value>Contol metadata <xsl:value-of select="$recordId"/> record</xtm:value>
            </xtm:name>
            <!-- Isaar 5.4.4 -->
            <xtm:occurrence>
                <xtm:type>
                    <xtm:topicRef href="#eac-cpf002"/>
                </xtm:type>
                <xtm:resourceData datatype="http://www.w3.org/2001/XMLSchema#string">
                    <xsl:value-of select="eac:maintenanceStatus"/>
                </xtm:resourceData>
            </xtm:occurrence>
            <!-- isaar 5.4.6 -->
            <xsl:apply-templates select="eac:maintenanceHistory/eac:maintenanceEvent">
                <xsl:with-param name="recordId" select="$recordId"/>
            </xsl:apply-templates>
        </xtm:topic>
        <!-- isaar 5.4.3 -->
        <xsl:apply-templates select="eac:conventionDeclaration">
            <xsl:with-param name="recordId" select="$recordId"/>
        </xsl:apply-templates>
        <!-- issar 5.4.7 -->
        <xsl:apply-templates select="eac:languageDeclaration/eac:language">
            <xsl:with-param name="recordId" select="$recordId"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="eac:languageDeclaration/eac:script">
            <xsl:with-param name="recordId" select="$recordId"/>
        </xsl:apply-templates>
        <!-- isaar 5.4.9 -->
        <xsl:apply-templates select=".//eac:eventDescription">
            <xsl:with-param name="recordId" select="$recordId"/>
        </xsl:apply-templates>
    </xsl:template>

    <!-- isaar 5.4.3 -->
    <xsl:template match="eac:conventionDeclaration">
        <xsl:param name="recordId"/>
        
        <xsl:variable name="ruleId">
            <xsl:choose>
                <xsl:when test="eac:citation/text()">
                    <xsl:value-of
                        select="concat('rule-',replace(eac:citation,'[^-_.:A-Za-z0-9]',''))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat('rule-',position(),'-',$recordId)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        
        <xsl:if test="not(eac:citation/text())">
            <xtm:topic id="{$ruleId}">
                <xtm:instanceOf>
                    <xtm:topicRef href="#eac-cpf034"/>
                </xtm:instanceOf>
                <xtm:name>
                    <xtm:value>
                        <xsl:value-of
                            select="concat('Rule ',position(),' associated to ',$recordId,' record')"
                        />
                    </xtm:value>
                </xtm:name>
                <xsl:apply-templates select="eac:descriptiveNote" mode="occurrence"/>
            </xtm:topic>
        </xsl:if>

        <xtm:association>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf003"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#eac-cpf004"/>
                </xtm:type>
                <xtm:topicRef href="#{$ruleId}"/>
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system001"/>
                </xtm:type>
                <xtm:topicRef href="#control-{$recordId}"/>
            </xtm:role>
        </xtm:association>
    </xsl:template>
    <xsl:template name="rule">
        <xtm:topic id="{concat('rule-',replace(eac:citation,'[^-_.:A-Za-z0-9]',''))}">
            <xtm:instanceOf>
                <xtm:topicRef href="#eac-cpf004"/>
            </xtm:instanceOf>
            <xsl:apply-templates select="eac:abbreviation"/>
            <xtm:name>
                <xtm:value>
                    <xsl:apply-templates select="eac:citation"/>
                </xtm:value>
            </xtm:name>
        </xtm:topic>
    </xsl:template>
    <xsl:template match="eac:abbreviation">
        <!-- todo topic name type -->
        <xtm:name>
            <xtm:value>
                <xsl:value-of select="."/>
            </xtm:value>
        </xtm:name>
    </xsl:template>

    <!-- isaar 5.4.6 -->
    <xsl:template match="eac:maintenanceEvent">
        <xsl:param name="recordId"/>
        <xsl:element name="xtm:occurrence">
            <xsl:if test="eac:eventDescription">
                <xsl:attribute name="reifier">
                    <xsl:value-of select="concat('#',$recordId,'-event-',position())"/>
                </xsl:attribute>
            </xsl:if>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf005"/>
            </xtm:type>
            <xtm:resourceData datatype="http://www.w3.org/2001/XMLSchema#string">
                <xsl:value-of
                    select="concat('[',eac:eventType,'] ',eac:agent,' - ',eac:eventDateTime)"/>
                <xsl:if test="eac:eventDateTime[not(text())]">
                    <xsl:value-of select="eac:eventDateTime/@standardDateTime"/>
                </xsl:if>
            </xtm:resourceData>
        </xsl:element>
    </xsl:template>

    <!-- 5.4.7 -->
    <xsl:template match="eac:language">
        <xsl:param name="recordId"/>
        <xtm:association>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf006"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system005"/>
                </xtm:type>
                <xtm:topicRef href="#language-{@languageCode}"/>
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system001"/>
                </xtm:type>
                <xtm:topicRef href="#control-{$recordId}"/>
            </xtm:role>
        </xtm:association>
    </xsl:template>
    <xsl:template match="eac:script">
        <xsl:param name="recordId"/>
        <xtm:association>
            <xtm:type>
                <xtm:topicRef href="#eac-cpf006"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system006"/>
                </xtm:type>
                <xtm:topicRef href="#script-{@scriptCode}"/>
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system001"/>
                </xtm:type>
                <xtm:topicRef href="#control-{$recordId}"/>
            </xtm:role>
        </xtm:association>
    </xsl:template>
    <xsl:template name="language">
        <xsl:variable name="langId" select="@languageCode"/>
        <xtm:topic id="language-{$langId}">
            <xtm:instanceOf>
                <xtm:topicRef href="#system005"/>
            </xtm:instanceOf>
            <xsl:for-each-group select="//eac:language[@languageCode=$langId]" group-by=".">
                <xsl:if test="text()">
                    <xtm:name>
                        <xtm:value>
                            <xsl:value-of select="."/>
                        </xtm:value>
                    </xtm:name>
                </xsl:if>
            </xsl:for-each-group>
            <xsl:apply-templates select="@languageCode"/>
        </xtm:topic>
    </xsl:template>
    <xsl:template name="script">
        <xsl:variable name="scriptId" select="@scriptCode"/>
        <xtm:topic id="script-{$scriptId}">
            <xtm:instanceOf>
                <xtm:topicRef href="#system006"/>
            </xtm:instanceOf>
            <xsl:for-each-group select="//eac:script[@scriptCode=$scriptId]" group-by=".">
                <xsl:if test="text()">
                    <xtm:name>
                        <xtm:value>
                            <xsl:value-of select="."/>
                        </xtm:value>
                    </xtm:name>
                </xsl:if>
            </xsl:for-each-group>
            <xsl:apply-templates select="@scriptCode"/>
        </xtm:topic>
    </xsl:template>
    <xsl:template match="@languageCode|@scriptCode">
        <!-- todo topic name type -->
        <xtm:name>
            <xtm:value>
                <xsl:value-of select="."/>
            </xtm:value>
        </xtm:name>
    </xsl:template>

    <!-- isaar 5.4.9 -->
    <xsl:template match="eac:eventDescription">
        <xsl:param name="recordId"/>
        <xsl:variable name="eventId" select="count(parent::*/preceding-sibling::*)+1"/>
        <xtm:topic id="{$recordId}-event-{$eventId}">
            <xtm:name>
                <xtm:value>Reification event <xsl:value-of select="$eventId"/></xtm:value>
            </xtm:name>
            <xtm:occurrence>
                <xtm:type>
                    <xtm:topicRef href="#eac-cpf007"/>
                </xtm:type>
                <xtm:resourceData datatype="http://www.w3.org/2001/XMLSchema#string">
                    <xsl:value-of select="."/>
                </xtm:resourceData>
            </xtm:occurrence>
        </xtm:topic>
    </xsl:template>

    <!-- general stuff -->
    <!-- paragraph, list etc -->
    <xsl:template match="eac:p">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="eac:outline">
        <!-- TODO -->
    </xsl:template>
    <xsl:template match="eac:list">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="eac:item"> * <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="eac:chronList">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="eac:chronItem"> * <xsl:apply-templates select="eac:date"/>
        <xsl:if test="eac:place">[<xsl:value-of select="eac:place"/>]</xsl:if>
        <xsl:text>&#xa0;</xsl:text>- <xsl:value-of select="eac:event"/>
    </xsl:template>
    <xsl:template match="eac:chronItem/eac:date">
        <xsl:choose>
            <xsl:when test="./text()">
                <xsl:value-of select="concat(.,' ')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="@standardDate">
                        <xsl:value-of select="concat(@standardDate,' ')"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:if test="@notAfter">
                            <xsl:value-of select="concat('Before ',@notAfter,' ')"/>
                        </xsl:if>
                        <xsl:if test="@notBefore">
                            <xsl:value-of select="concat('After ',@notBefore),' '"/>
                        </xsl:if>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="eac:citation">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="eac:descriptiveNote">
        <xsl:apply-templates/>
    </xsl:template>

    <!-- link -->
    <xsl:template match="@xlink:href">
        <xtm:occurrence>
            <xtm:type>
                <xtm:topicRef href="#system009"/>
            </xtm:type>
            <xtm:resourceData datatype="http://www.w3.org/2001/XMLSchema#anyURI">
                <xsl:value-of select="."/>
            </xtm:resourceData>
        </xtm:occurrence>
    </xsl:template>

    <!-- EAC-CPF construct -->
    <xsl:template name="eac-construct">
        <xtm:topic id="eac-cpf001">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar541"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Authority record identifier</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf002">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar544"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Status</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf003">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar543"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Rules and/or conventions</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf004">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Rules</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf005">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar546"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Dates of creation, revision or deletion</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf006">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar547"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Languages and scripts</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf007">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar549"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Maintenance notes</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf008">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar522"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>History</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf009">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar528"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>General Context</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf010">
            <xtm:subjectIdentifier href="http://psi.ontopedia.net/Place"/>
            <xtm:subjectIdentifier href="http://dbpedia.org/resource/Place"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Place</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf011">
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Altitude</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf012">
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Latitudine</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf013">
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Longitude</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf014">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar523"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Places</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf015">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Legal Status</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf016">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar524"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Legal Status</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf017">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar521"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Exist Dates</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf018">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar525"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Functions, occupations and activities</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf019">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Function</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf020">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Occupation</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf021">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Activity</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf022">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar526"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Mandates/Sources of authority</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf023">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Mandate</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf024">
            <xtm:subjectIdentifier href="http://gilgamesh.unipv.it/psi/isaar/#isaar527"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Internal structures/Genealogy</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf-agent">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Agent</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf-person">
            <xtm:subjectIdentifier href="http://psi.ontopedia.net/Person"/>
            <xtm:subjectIdentifier href="http://dbpedia.org/resource/Person"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Person</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf-family">
            <xtm:subjectIdentifier href="http://dbpedia.org/resource/Family"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Family</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="eac-cpf-corporateBody">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Corporate Body</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:association>
            <xtm:type>
                <xtm:topicRef href="#system017"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system019"/>
                </xtm:type>
                <xtm:topicRef href="#eac-cpf-agent"/>
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system020"/>
                </xtm:type>
                <xtm:topicRef href="#eac-cpf-corporateBody"/>
            </xtm:role>
        </xtm:association>
        <xtm:association>
            <xtm:type>
                <xtm:topicRef href="#system017"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system019"/>
                </xtm:type>
                <xtm:topicRef href="#eac-cpf-agent"/>
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system020"/>
                </xtm:type>
                <xtm:topicRef href="#eac-cpf-family"/>
            </xtm:role>
        </xtm:association>
        <xtm:association>
            <xtm:type>
                <xtm:topicRef href="#system017"/>
            </xtm:type>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system019"/>
                </xtm:type>
                <xtm:topicRef href="#eac-cpf-agent"/>
            </xtm:role>
            <xtm:role>
                <xtm:type>
                    <xtm:topicRef href="#system020"/>
                </xtm:type>
                <xtm:topicRef href="#eac-cpf-person"/>
            </xtm:role>
        </xtm:association>
    </xsl:template>

    <!-- System construct -->
    <xsl:template name="system-construct">
        <xtm:topic id="system001">
            <xtm:subjectIdentifier href="http://psi.ontopia.net/ontology/topic-map"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Topic map</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system002">
            <xtm:subjectIdentifier href="http://psi.ontopia.net/ontology/topic-type"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Topic Type</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system003">
            <xtm:subjectIdentifier href="http://psi.ontopia.net/ontology/occurrence-type"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Occurrence type</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system004">
            <xtm:subjectIdentifier href="http://psi.ontopia.net/ontology/association-type"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Association type</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system005">
            <xtm:subjectIdentifier href="http://www.topicmaps.org/xtm/1.0/language.xtm#language"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Language</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system006">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Script</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system007">
            <xtm:subjectIdentifier href="http://purl.org/dc/elements/1.1/description"/>
            <xtm:subjectIdentifier href="http://psi.ontopedia.net/description"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Description</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system008">
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Citation</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system009">
            <xtm:instanceOf>
                <xtm:topicRef href="#system003"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Link</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system010">
            <xtm:subjectIdentifier href="http://psi.semagia.com/datetime/calendar_date"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Date</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system011">
            <xtm:subjectIdentifier href="http://psi.semagia.com/ISO8601"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#eac-cpf004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>ISO 8601</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system012">
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Period</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system013">
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Begin data</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system014">
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>End data</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system015">
            <xtm:subjectIdentifier href="http://psi.topincs.com/iso13250/topic-name-type"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Topic name type</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system016">
            <xtm:subjectIdentifier href="http://psi.topicmaps.org/iso13250/model/topic-name"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system015"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Topic name</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system017">
            <xtm:subjectIdentifier href="http://psi.topicmaps.org/iso13250/model/supertype-subtype"/>
            <xtm:subjectIdentifier
                href="http://www.topicmaps.org/xtm/1.0/core.xtm#superclass-subclass"/>
            <xtm:subjectIdentifier
                href="http://www.techquila.com/psi/hierarchy/#hierarchical-relation-type"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system004"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>The supertype-subtype relationship</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system018">
            <xtm:subjectIdentifier href="http://psi.topincs.com/iso13250/association-role-type"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system002"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Association role type</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system019">
            <xtm:subjectIdentifier href="http://psi.topicmaps.org/iso13250/model/supertype"/>
            <xtm:subjectIdentifier href="http://www.topicmaps.org/xtm/1.0/core.xtm#superclass"/>
            <xtm:subjectIdentifier
                href="http://www.techquila.com/psi/hierarchy/#superordinate-role-type"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system018"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Supertype</xtm:value>
            </xtm:name>
        </xtm:topic>
        <xtm:topic id="system020">
            <xtm:subjectIdentifier href="http://psi.topicmaps.org/iso13250/model/subtype"/>
            <xtm:subjectIdentifier href="http://www.topicmaps.org/xtm/1.0/core.xtm#subclass"/>
            <xtm:subjectIdentifier
                href="http://www.techquila.com/psi/hierarchy/#subordinate-role-type"/>
            <xtm:instanceOf>
                <xtm:topicRef href="#system018"/>
            </xtm:instanceOf>
            <xtm:name>
                <xtm:value>Subtype</xtm:value>
            </xtm:name>
        </xtm:topic>
    </xsl:template>
</xsl:stylesheet>
